package ssf.core.common;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

import org.apache.commons.logging.*;
import org.apache.struts.action.*;
import org.apache.struts.config.*;

import ssf.core.Constants;

/**
 * 异常拦截器，拦截系统发生的异常并转到指定页面呈现
 * 
 * @author Nanlei
 * 
 */
public class ExceptionInterceptor extends ExceptionHandler {
	protected final Log log = LogFactory.getLog(getClass());

	@Override
	/**
	 * 异常处理方法
	 */
	public ActionForward execute(Exception e, ExceptionConfig ecfg,
			ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		// 创建StringWriter对象
		StringWriter sw = new StringWriter();
		// 将异常堆栈信息放入sw中
		e.printStackTrace(new PrintWriter(sw));
		// 设置异常名称
		request.setAttribute(Constants.CONTEXT_EXCEPTION, e.toString());
		// 设置异常堆栈信息
		request.setAttribute(Constants.CONTEXT_EXCEPTION_INFO, sw.toString());
		// 转向struts-config.xml中异常处理配置信息
		return mapping.findForward(Constants.EXCEPTION);
	}

	@Override
	/**
	 * 写日志方法
	 */
	protected void logException(Exception e) {
		String message = "Caught Exception: ";
		log.error(message, e);
	}

}
